package com.tinyengine.it.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyengine.it.common.utils.JavaClassNameValidator;
import com.tinyengine.it.mapper.DbLogicalFieldsInfoMapper;
import com.tinyengine.it.mapper.DbLogicalInfoMapper;
import com.tinyengine.it.model.entity.DbLogicalFieldsInfo;
import com.tinyengine.it.model.entity.DbLogicalInfo;
import com.tinyengine.it.model.entity.SysTermCache;
import com.tinyengine.it.model.vo.DbLogicalFieldsInfoVo;
import com.tinyengine.it.model.vo.DbLogicalInfoVo;
import com.tinyengine.it.service.db.DbLogicalInfoService;
import com.tinyengine.it.service.db.SysTermCacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【i_db_logical_info(数据逻辑实体信息)】的数据库操作Service实现
 * @createDate 2025-03-15 15:42:33
 */
@Service
@RequiredArgsConstructor
public class DbLogicalInfoServiceImpl extends ServiceImpl<DbLogicalInfoMapper, DbLogicalInfo>
        implements DbLogicalInfoService {

    private final DbLogicalInfoMapper dbLogicalInfoMapper;
    private final DbLogicalFieldsInfoMapper dbLogicalFieldsInfoMapper;
    private final SysTermCacheService sysTermCacheService;

    @Override
    @Transactional
    public DbLogicalInfoVo saveInfo(DbLogicalInfoVo dbLogicalInfoVo) {
        DbLogicalInfo dbLogicalInfo = BeanUtil.copyProperties(dbLogicalInfoVo, DbLogicalInfo.class);
        dbLogicalInfoMapper.insertOrUpdate(dbLogicalInfo);

        List<DbLogicalFieldsInfo> dbLogicalFieldsInfos = BeanUtil.copyToList(dbLogicalInfoVo.getLogicalFieldsInfoVo(), DbLogicalFieldsInfo.class);
        dbLogicalFieldsInfos.forEach(e -> {
            e.setTableId(dbLogicalInfo.getId());
            String fieldValid = JavaClassNameValidator.isValidClassName(e.getCode());
            if (StrUtil.isNotBlank(fieldValid)) {
                throw new RuntimeException(fieldValid);
            }
        });

        List<Integer> fields = dbLogicalFieldsInfos.stream().filter(f -> ObjUtil.isNotNull(f.getId())).map(m -> m.getId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(fields) && fields.size() > 0) {
            dbLogicalFieldsInfoMapper.delete(Wrappers.<DbLogicalFieldsInfo>lambdaQuery().eq(DbLogicalFieldsInfo::getTableId, dbLogicalInfo.getId()).notIn(DbLogicalFieldsInfo::getId, fields));
        } else {
            dbLogicalFieldsInfoMapper.delete(Wrappers.<DbLogicalFieldsInfo>lambdaQuery().eq(DbLogicalFieldsInfo::getTableId, dbLogicalInfo.getId()));
        }

        dbLogicalFieldsInfoMapper.insertOrUpdate(dbLogicalFieldsInfos);

        //跟新系统名称缓存库
        List<SysTermCache> sysTermCaches = dbLogicalFieldsInfos.stream().map(m -> new SysTermCache().setName(m.getName()).setEnName(StrUtil.toCamelCase(m.getCode()))).collect(Collectors.toList());
        sysTermCacheService.updateSysTermCacheInfo(sysTermCaches);

        //返回所有数据包含id
        List<DbLogicalFieldsInfoVo> infoVos = BeanUtil.copyToList(dbLogicalFieldsInfos, DbLogicalFieldsInfoVo.class);
        DbLogicalInfoVo logicalInfoVo = BeanUtil.copyProperties(dbLogicalInfo, DbLogicalInfoVo.class);
        logicalInfoVo.setLogicalFieldsInfoVo(infoVos);

        return logicalInfoVo;

    }
}




